package cn.jsc.basic.service.impl;

import cn.jsc.basic.mapper.BaseMapper;
import cn.jsc.basic.query.BaseQuery;
import cn.jsc.basic.service.IBaseService;
import cn.jsc.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> baseMapper;
    @Override
    @Transactional
    public void add(T t) {
        baseMapper.save(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        baseMapper.remove(id);
    }

    @Override
    public T getById(Long id) {
        return baseMapper.loadById(id);
    }

    @Override
    public List<T> getAll() {
        return baseMapper.loadAll();
    }
    /**
     * 返回分页的数据
     * @param query
     * @return
     */
    @Override
    public PageList<T> queryData(BaseQuery query) {
        //1.获取总条数
        Long count = baseMapper.queryCount(query);
        //2.判断总条数如果为0，直接返回默认对象
        if(count==0){
            return new PageList<>();
        }
        //3.查询当前页的数据
        List<T> rows = baseMapper.queryData(query);
        //4.返回分页数据
        PageList<T> pageList = new PageList<>(count, rows);
        return pageList;
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    @Transactional
    public void patchDelete(List<Long> ids) {
        baseMapper.patchDelete(ids);

    }
}
